package studio.mandysa.jiuwo.ktx

import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import studio.mandysa.jiuwo.adapter.RecyclerAdapter

/**
 * 将[RecyclerView]的[RecyclerView.LayoutManager]设置为[LinearLayoutManager]
 * @see [LinearLayoutManager]
 */
fun RecyclerView.linear(
    @RecyclerView.Orientation orientation: Int = RecyclerView.VERTICAL,
): RecyclerView {
    layoutManager = LinearLayoutManager(context, orientation, false)
    return this
}

/**
 * 将[RecyclerView]的[RecyclerView.LayoutManager]设置为[GridLayoutManager]
 * @see [GridLayoutManager]
 */
fun RecyclerView.grid(
    spanCount: Int = 1,
    @RecyclerView.Orientation orientation: Int = RecyclerView.VERTICAL
): RecyclerView {
    layoutManager = GridLayoutManager(context, spanCount, orientation, false)
    return this;
}

/**
 * 将[RecyclerView]的[RecyclerView.LayoutManager]设置为[StaggeredGridLayoutManager]
 * @see [StaggeredGridLayoutManager]
 */
fun RecyclerView.staggered(
    spanCount: Int = 1,
    @RecyclerView.Orientation orientation: Int = RecyclerView.VERTICAL
): RecyclerView {
    layoutManager = StaggeredGridLayoutManager(spanCount, orientation)
    return this
}

/**
 * 为[RecyclerView]设置[RecyclerAdapter]
 */
fun RecyclerView.setup(block: RecyclerAdapter.(RecyclerView) -> Unit): RecyclerAdapter {
    return RecyclerAdapter().also {
        adapter = it
        block.invoke(it, this)
    }
}

/**
 * 添加[model]到 [RecyclerAdapter.headers]
 * @param model 要添加的model
 */
fun RecyclerView.addHeader(model: Any) {
    addHeader(model, recyclerAdapter.headerSize)
}

/**
 * 添加[model]到 [RecyclerAdapter.headers]
 * @param model 要添加的model
 * @param position 插入位置
 */
@Suppress("UNCHECKED_CAST")
fun RecyclerView.addHeader(model: Any, position: Int) {
    if (recyclerAdapter.headers != null) {
        val list = recyclerAdapter.headers!!.toMutableList()
        when (model) {
            is List<*> -> {
                list.addAll(position, model as List<Any>)
            }
            else -> {
                list.add(position, model)
            }
        }
        recyclerAdapter.headers = list
        return
    }
    recyclerAdapter.headers = when (model) {
        is List<*> -> {
            model as List<Any>
        }
        else -> {
            listOf(model)
        }
    }
}

/**
 * 添加[model]到 [RecyclerAdapter.models]
 * @param model 要添加的model
 */
fun RecyclerView.addModels(model: Any) {
    addModels(model, recyclerAdapter.modelSize)
}

/**
 * 添加[model]到 [RecyclerAdapter.models]
 * @param model 要添加的model
 * @param position 插入位置
 */
@Suppress("UNCHECKED_CAST")
fun RecyclerView.addModels(model: Any, position: Int) {
    if (recyclerAdapter.models != null) {
        val list = recyclerAdapter.models!!.toMutableList()
        when (model) {
            is List<*> -> {
                list.addAll(position, model as List<Any>)
            }
            else -> {
                list.add(position, model)
            }
        }
        recyclerAdapter.models = list
        return
    }
    recyclerAdapter.models = when (model) {
        is List<*> -> {
            model as List<Any>
        }
        else -> {
            listOf(model)
        }
    }
}

/**
 * 添加[model]到 [RecyclerAdapter.footers]
 * @param model 要添加的model
 */
fun RecyclerView.addFooter(model: Any) {
    addFooter(model, recyclerAdapter.footerSize)
}

/**
 * 添加[model]到 [RecyclerAdapter.footers]
 * @param model 要添加的model
 * @param position 插入位置
 */
@Suppress("UNCHECKED_CAST")
fun RecyclerView.addFooter(model: Any, position: Int) {
    if (recyclerAdapter.footers != null) {
        val list = recyclerAdapter.footers!!.toMutableList()
        when (model) {
            is List<*> -> {
                list.addAll(position, model as List<Any>)
            }
            else -> {
                list.add(position, model)
            }
        }
        recyclerAdapter.footers = list
        return
    }
    recyclerAdapter.footers = when (model) {
        is List<*> -> {
            model as List<Any>
        }
        else -> {
            listOf(model)
        }
    }
}

val RecyclerView.recyclerAdapter
    get() = (adapter as RecyclerAdapter)
